import TIM from 'tim-wx-sdk';
import TIMUploadPlugin from 'tim-upload-plugin';
import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';
import { genTestUserSig } from '../../components/TUIKit/debug/GenerateTestUserSig';


Page({
  data: {
    config: {
      userID: '', //User ID
      SDKAPPID: 1400810789, // Your SDKAppID
      SECRETKEY: '0cec25940a6e8b81c8e2f75b45796101920657dc1789679e10969f04ce037e52', // Your secretKey
      EXPIRETIME: 604800,
    }
  },

  onLoad() {
    wx.getStorage({
      key: 'myInfo',
      success: (result) => {
        console.log('openID', result.data.openid)
        this.setData({
          config: {
            userID: result.data.openid, //User ID
            SDKAPPID: 1400810789, // Your SDKAppID
            SECRETKEY: '0cec25940a6e8b81c8e2f75b45796101920657dc1789679e10969f04ce037e52', // Your secretKey
            EXPIRETIME: 604800,
          }
        })
        console.log('result.data.openid', result.data.openid)
      },
      fail: () => { },
      complete: () => { }
    })
    setTimeout(() => {
      const userSig = genTestUserSig(this.data.config).userSig
      console.table(userSig)
      wx.$TUIKit = TIM.create({
        SDKAppID: this.data.config.SDKAPPID
      })
      wx.$chat_SDKAppID = this.data.config.SDKAPPID;
      wx.$chat_userID = this.data.config.userID;
      wx.$chat_userSig = userSig;
      wx.$TUIKitTIM = TIM;
      wx.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });
      wx.$TUIKit.registerPlugin({ 'tim-profanity-filter-plugin': TIMProfanityFilterPlugin });
      wx.$TUIKit.login({
        userID: this.data.config.userID,
        userSig
      });
      wx.setStorage({
        key: 'currentUserID',
        data: [],
      });
      wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.SDK_READY, this.onSDKReady, this);
    }, 200)
  },
  onUnload() {
    wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.SDK_READY, this.onSDKReady, this);
  },
  onSDKReady() {
    const TUIKit = this.selectComponent('#TUIKit');
    TUIKit.init();
  }
});
